package fragments;


import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseFragment;
import com.example.administrator.newxlmedical.R;

import java.util.ArrayList;

import beans.PostBean;
import beans.RegisterInfoBean;
import beans.UserInfoBean;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import dialogs.FactoryRegisterInfoDialog;
import netframe.GetRetrofit;
import netframe.NetAddress;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import singleton.SingletonClass;
import utils.TimeUtil;
import utils.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public class FactoryRegisterFragment extends BaseFragment {


    @BindView(R.id.et_factory_register_id_number)
    EditText etName;

    @BindView(R.id.tv_factory_register_register)
    TextView tvRegister;

    @BindView(R.id.iv_factory_rigister_back)
    ImageView ivBack;
    private RegisterInfoBean bean;

    Unbinder unbinder;
    private MyHandler allHandler;
    private int type;
    private UserInfoBean userInfoBean;

    public FactoryRegisterFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_factory_register, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    protected void initView() {
        super.initView();
        bean = (RegisterInfoBean) getArguments().getSerializable(Constants.FACTORY_REGISTER_FRAGMENT);
        allHandler = SingletonClass.getSingleton(MyHandler.class);
    }

    @OnClick({R.id.tv_factory_register_register, R.id.iv_factory_rigister_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_factory_register_register:
                String name = etName.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.showToast(mContext, "请输入姓名!");
                } else {
                    bean.setName(name);
                    userInfoBean = new UserInfoBean();
                    userInfoBean.setName(bean.getName());
                    userInfoBean.setSubTime(TimeUtil.getMillisecondDate());
                    userInfoBean.setDelFlag(0);
                    userInfoBean.setDepartment("星联科技");
                    userInfoBean.setEnable(true);
                    userInfoBean.setRoleInfoId(bean.getRegisterRole());
                    searchName(name);

                }
                break;

            case R.id.iv_factory_rigister_back:
                allHandler.sendEmptyMessage(HandlerConstant.REGISTER_SUCCESS_FRAGMENT_SURE);
                break;
        }

    }

    private void searchName(String userName) {
        GetRetrofit.getInstance().getUserInfo(NetAddress.token, NetAddress.machineCode, "select * from UserInfo where Name = '" + userName + "'")
                .enqueue(new Callback<ArrayList<UserInfoBean>>() {
                    @Override
                    public void onResponse(Call<ArrayList<UserInfoBean>> call, Response<ArrayList<UserInfoBean>> response) {
                        ArrayList<UserInfoBean> body = response.body();
                        if (body != null && body.size() != 0) {
                            ToastUtil.showToast(getContext(),"姓名已被注册!");
                        } else {
                            showDialog();
                        }

                    }

                    @Override
                    public void onFailure(Call<ArrayList<UserInfoBean>> call, Throwable t) {
                    }
                });
    }

    private void postUser() {
        GetRetrofit.getInstance().postUserInfo(NetAddress.token, NetAddress.machineCode, userInfoBean)
                .enqueue(new Callback<PostBean>() {
                    @Override
                    public void onResponse(Call<PostBean> call, Response<PostBean> response) {
                        allHandler.sendEmptyMessage(HandlerConstant.REFRESH_NET); // 管理页面刷新数据
                    }

                    @Override
                    public void onFailure(Call<PostBean> call, Throwable t) {
                    }
                });

    }

    private void showDialog() {

        new FactoryRegisterInfoDialog(mContext, R.style.dialog, bean, new FactoryRegisterInfoDialog.OnClickListener() {
            @Override
            public void onSureClickListener(FactoryRegisterInfoDialog dialog) {

                postUser();

                Message message = allHandler.obtainMessage();
                message.obj = bean;
                message.what = HandlerConstant.REGISTER_TYPE;
                allHandler.sendMessage(message);
                dialog.dismiss();
            }
        }).show();
    }
}
